<?php
require_once "db/db.php";
?>
<!-- sample style for text autolist -->
		<style type="text/css" media="screen">
			.form_tags { margin-bottom: 5px; }

			/* Setting widget width example */
			.textboxlist { width: 250px; }

			/* Preloader for autocomplete */
			.textboxlist-loading { background: url('images/spinner.gif') no-repeat 380px center; }

			/* Autocomplete results styling */
			.form_friends .textboxlist-autocomplete-result { overflow: hidden; zoom: 1; }
			.form_friends .textboxlist-autocomplete-result img { float: left; padding-right: 10px; }
			
		</style>
		
					<form action = "" method = "post">
						
						<table style = 'text-align: left;'>
							<tr>
								<th>Tôi tên là: </th>
								<td class = "input"><input type = "text" name = "full_name"/></td>								
							</tr>
							<tr>
								<th>Giới tính: </th>
								<td class = "input">
									<select name = "gender">
										<option selected = "default" value = "-1">Tôi là...</option>
										<option value = "1">Nam</option>
										<option value = "0">Nữ</option>
									</select> 
								</td>
							</tr>
							<tr>
								<th>Sinh nhật: </th>
								<td>
									<select name = "day">
										<option selected = "default" value = "-1">Ngày: </option>
										<?php
										for($i = 1; $i<=31; $i++){
											echo '<option value = "'.$i.'">'.$i.'</option>';
										}
										?>
										
									</select>
									
									<select name = "month">
										<option selected = "default" value = "-1">Tháng: </option>
										<?php
										for($i = 1; $i<=12; $i++){
											echo '<option value = "'.$i.'">'.$i.'</option>';
										}
										?>
										
									</select>
									
									<select name = "year">
										<option selected = "default" value = "-1">Năm: </option>
										<?php
										$year = Date("Y");
										for($i = $year; $i>=1900; $i--){
											echo '<option value = "'.$i.'">'.$i.'</option>';
										}
										?>
										
									</select>
								</td>
							</tr>
							<tr>
								<th>Email: </th>
								<td class = "input"><input type = "text" name = "email"/></td>
							</tr>
							<tr>
								<th>Mật khẩu: </th>
								<td class = "input"><input type = "password" name = "pass"/></td>
							</tr>
							<tr>
								<th>Nhập lại mật khẩu: </th>
								<td class = "input"><input type = "password" name = "repass"/></td>
							</tr>
							<tr>
								<th>Mã: </th>
								<td><img src = "include/captcha.php"/></td>
							</tr>
							<tr>
								<th>Nhập mã: </th>
								<td class = "input"><input type = "text" name = "captcha"/></td>
							</tr>
							<tr>
								<th><a href = "#" title = "Nơi bạn đang học hoặc đã từng học.">Học trường?: </a></th>
								<td><input type = "text" name = "school" id = "form_tags_input_3"/></td>
							</tr>
							<tr><td></td></tr>
							<tr><td></td></tr>
							<tr>
								<td></td>
								<td class = "button"><input type = "submit" name = "submit" value = "Đăng kí"/></td>
							</tr>
						</table>						
							
					</form>	
					<?php
					Singup();
					?>
					
					<!-- required for TextboxList -->
		<script src="js/GrowingInput.js" type="text/javascript" charset="utf-8"></script>
				
		<script src="js/TextboxList.js" type="text/javascript" charset="utf-8"></script>		
		<script src="js/TextboxList.Autocomplete.js" type="text/javascript" charset="utf-8"></script>
		<!-- required for TextboxList.Autocomplete if method set to 'binary' -->
		<script src="js/TextboxList.Autocomplete.Binary.js" type="text/javascript" charset="utf-8"></script>		
		
		<!-- sample initialization -->

		<script type="text/javascript" charset="utf-8">		
			$(function(){
							
				// Autocomplete initialization
				var t4 = new $.TextboxList('#form_tags_input_3', {unique: true, plugins: {autocomplete: {}}});
							
				// sample data loading with json, but can be jsonp, local, etc. 
				// the only requirement is that you call setValues with an array of this format:
				// [
				//	[id, bit_plaintext (on which search is performed), bit_html (optional, otherwise plain_text is used), autocomplete_html (html for the item displayed in the autocomplete suggestions dropdown)]
				// ]
				// read autocomplete.php for a JSON response exmaple
				t4.getContainer().addClass('textboxlist-loading');				
				$.ajax({url: 'ajax/autocomplete-school.php', dataType: 'json', success: function(r){
					t4.plugins['autocomplete'].setValues(r);
					t4.getContainer().removeClass('textboxlist-loading');
				}});						
			});
		</script>
<?
function Singup()
	{
		if(isset($_POST['submit']))
			{
				//check infor user input singup form
				if($_POST['full_name'] && $_POST['gender'] != "-1" && $_POST['email'] &&
				$_POST['pass'] && $_POST['repass'] && $_POST['captcha'] && $_POST['school']&&
					$_POST['day'] != "-1" && $_POST['month'] != "-1" && $_POST['year'] != "-1"){
					//check email user input textbox
					$check_email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
					
					if($check_email){
						if($_POST['pass'] == $_POST['repass']){
							if($_POST['captcha'] == $_SESSION['key']){
								//check email in database
								$check_email = DbSelect("SELECT `email` FROM `oUsers` 
								WHERE `email` = '".$_POST['email']."'");
								if(!$check_email){
									//save infor
									$full_name = strip_tags($_POST['full_name']);
									$gender = strip_tags($_POST['gender']);
									$birthday = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
									$email = strip_tags($_POST['email']);
									$pass = strip_tags($_POST['pass']);
									//sha1 pass
									$pass = sha1($pass);
									$school = strip_tags($_POST['school']);								
									$date = date("Y-m-d");
									//require random.php
									require_once "include/random.php";
																										
									//$email code
									$email_code = RandNumber(4);
																		
									//send mail singup
									require_once "include/send-mail-singup.php";
									
									//check value schoolId get database
									if(!preg_match('/[a-zA-Z]+/', $school)){
										//save name school
										$school_name = "";
										$select_school_id = DbSelect("SELECT `id`, `name` FROM `oGroups` WHERE `id` = '$school'");
										foreach($select_school_id as $values){
											$school_name = $values['name'];
										}										
										//query
										
										$query_insert = "INSERT INTO `oUsers`(
										`fullName`, `email`, `emailCode`, `password`, `birthday`, `gender`,
										`school`, `schoolId`, `registerDay`
										) 
										VALUES (
										'$full_name', '$email', '$email_code',
										'$pass', '$birthday', '$gender', '$school_name', '$school', '$date'
										)";
										
										//insert 
										$insert = DbInsert($query_insert);
										if($insert){
											//call function send mail
											SendMailSingup($full_name, $email, $email_code);
											echo "<p class = 'complete'>Đăng kí thành công.</p>";											
										}
										else
											echo "<p class = 'error'>Đăng kí thất bại, vui lòng quay lại sau.</p>";
									}	
									//new school	
									else{
										//random id for groups										
										$school_id = RandNumber(6);
										
										//query isnsert users
										$query_insert = "INSERT INTO `oUsers`(
										`fullName`, `email`, `emailCode`, `password`,`birthday`, `gender`,
										`school`, `schoolId`, `registerDay`
										) 
										VALUES (
										'$full_name', '$email', '$email_code',
										'$pass', '$birthday', '$gender', '$school', '$school_id', '$date'
										)";
										//query insert groups
										$query_insert_g = "INSERT INTO `oGroups`(
											`id`, `name`, `status`
												) VALUES(
													'$school_id', '$school', '1')"
												;
										//insert 
										$insert = DbInsert($query_insert);
										$insert_group = DbInsert($query_insert_g);
										
										if($insert){
											//call function send mail
											SendMailSingup($full_name, $email, $email_code);
											echo "<p class = 'complete'>Đăng kí thành công.</p>";
										}
										else
											echo "<p class = 'error'>Đăng kí thất bại, vui lòng quay lại sau.</p>";
									}									
																		
								}
								else
									echo "<p class = 'error'>Email đã tồn tại, vui lòng kiểm tra lại.</p>";		
							}	
							else
								echo "<p class = 'error'>Nhập mã sai, vui lòng nhập lại.</p>";						
						}
						else
							echo "<p class = 'error'>Nhập lại mật khẩu sai, vui lòng nhập lại.</p>";	
					}
					else
						echo "<p class = 'error'>Email không đúng, vui lòng nhập lại.</p>";	
				}
				else
					echo "<p class = 'error'>Vui lòng nhập đầy đủ thông tin.</p>";	
				
			}
	}
?>	